<?php
namespace App\Admin\Models;
use Common\BaseModel;
class Adminusers extends BaseModel
{
    protected $res;
    protected $redis_key="admin_";
    public function getSource()
    {
        return "f_admin_users";
    }
    public function initialize()
    {
        parent::initialize();
    }


    public function admin_list($params=[]){
        if(isset($params['keywords']) && !empty($params['keywords'])){
            $condition = "title like '%{$params['keywords']}%'";
        }else{
            $condition = '';
        }
        $cate_list = $this::find(
            array(
                $condition,
                "order" => 'id desc',
            ));
        return $cate_list;
    }


    /**
     * 验证用户名
     * @param $id
     * @return mixed
     */
    public function find_by_name_validate($name,$id=0){
        if($id){
            $condition = "name='{$name}' AND id<>{$id}";
        }else{
            $condition= "name='{$name}'";
        }
        $result = $this->find(
            array(
                $condition
            )
        );
        if($result){
            return $result->toarray();
        }else{
            return [];
        }
    }



    public function find_by_id($id){
        return $this->findById($id);
    }


    /**
     * @func 根据手机号获取用户的信息
     */
    public function find_by_phone($phone){
        $user_info = $this->findFirstByPhone($phone);
        if(!$user_info){
            return false;
        }else{
            return $user_info->toarray();
        }
    }



    /**
     * @func 根据用户id获取用户信息
     */
    public function find_by_firstId($user_id){
        $user_info = $this->findFirstById($user_id);
        if(!$user_info){
            return false;
        }else{
            return $user_info->toarray();
        }
    }



    /**
     * @func 根据用户名查找用户
     */
    public function find_by_name($name){
        $user_info = $this->findFirstByName($name);
        if(!$user_info){
            return false;
        }else{
            return $user_info->toarray();
        }
    }

    /**
     * @func 根据邀请码获取用户
     */
    public function find_by_inviteCode($invite_code){
        $conditions['conditions'] = "invite_code = '{$invite_code}' or  phone = '{$invite_code}'";
        $user_info = $this->findFirst($conditions);
        if(!$user_info){
            return false;
        }else{
            return $user_info->toarray();
        }
    }


    /**
     * @func 更新/插入数据
     * @param $data要更新或者要插入的数据
     * @return boolean 插入成功的结果
     */
    public function save_user($data){
        $data['updated_at'] = date('Y-m-d H:i:s',time());
        $result = $this->save($data);
        //var_dump($this->getMessages());
        return $result;
    }
}
